//
//  XiuGaiMiMaViewController.m
//  ZhiBoYuan
//
//  Created by coder_di on 2018/4/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "XiuGaiMiMaViewController.h"

@interface XiuGaiMiMaViewController ()

@property(nonatomic, strong)UITextField *yanzhengmaTextField;
@property(nonatomic, strong)UITextField *nePassWordTextField;
@property(nonatomic, strong)UITextField *ne2PassWordTextField;
@property (nonatomic, strong)UIButton *huoquYanZM;//获取验证码
@property (nonatomic, assign)int codeNum;


@end

@implementation XiuGaiMiMaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _codeNum = 60;
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"修改密码";
    [self setUI];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOKbtn)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    
    
    
}

- (void)onClickedOKbtn {
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{@"phone":_phoneNumber,
                       @"sms_code":_yanzhengmaTextField.text,
                       @"type":@"backPwd",
                       @"new_password1":_nePassWordTextField.text,
                       @"new_password2":_ne2PassWordTextField.text,
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Ucenter&a=changePassword",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] intValue] == 200) {
            [MBProgressHUD showTextOnly:self.view message:@"修改成功" delay:2.0];
            
             [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            [MBProgressHUD showTextOnly:self.view message:responseObject[@"msg"] delay:2.0];
            
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
   
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_yanzhengmaTextField resignFirstResponder];
    [_nePassWordTextField resignFirstResponder];
    [_ne2PassWordTextField resignFirstResponder];
}

-(void)setUI{
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(20 * KscreenWidth, 30 * KscreenHeight + NAVIGATION_BAR_HEIGHT, 100 * KscreenWidth, 30 *KscreenHeight)];
    lable.text = @"账号";
    lable.font = [UIFont systemFontOfSize:18 * KscreenWidth];
    [self.view addSubview:lable];
    
    UILabel *phoneNumLable = [[UILabel alloc]initWithFrame:CGRectMake(120 * KscreenWidth, 30 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth - 140 * KscreenWidth, 30 * KscreenHeight)];
    phoneNumLable.font = [UIFont systemFontOfSize:18 * KscreenWidth];
    NSLog(@"%@",_phoneNumber);
    phoneNumLable.text = _phoneNumber;
    [self.view addSubview:phoneNumLable];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(20 * KscreenWidth, 80 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth - 40 * KscreenWidth, 1 * KscreenHeight)];
    line.backgroundColor = ZhuTiColor;
    [self.view addSubview:line];
    
    
    UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(20 * KscreenWidth, 100 * KscreenHeight + NAVIGATION_BAR_HEIGHT, 100 * KscreenWidth, 30 *KscreenHeight)];
    lable1.text = @"验证码";
    lable1.font = [UIFont systemFontOfSize:18 * KscreenWidth];
    [self.view addSubview:lable1];
    
   _yanzhengmaTextField = [[UITextField alloc]initWithFrame:CGRectMake(120 * KscreenWidth, 100 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth - 240 * KscreenWidth, 30 * KscreenHeight)];
    _yanzhengmaTextField.font = [UIFont systemFontOfSize:18 * KscreenWidth];
    _yanzhengmaTextField.placeholder = @"输入短信验证码";
    [self.view addSubview:_yanzhengmaTextField];
    
    _huoquYanZM = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _huoquYanZM.frame = CGRectMake(ScreenWidth - 120 * KscreenWidth,  105 * KscreenHeight + NAVIGATION_BAR_HEIGHT, 100 * KscreenWidth, 20 * KscreenHeight);
    [_huoquYanZM setTitle:@"获取验证码" forState:0];
    [_huoquYanZM setTitleColor:ZhuTiColor forState:0];
    [_huoquYanZM addTarget:self action:@selector(sendYanZhengMaAction) forControlEvents:(UIControlEventTouchUpInside)];
    _huoquYanZM.font = [UIFont systemFontOfSize:15 * KscreenWidth];
    [self.view addSubview:_huoquYanZM];
    
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(20 * KscreenWidth, 150 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth - 40 * KscreenWidth, 1 * KscreenHeight)];
    line1.backgroundColor = hui2Color;
    [self.view addSubview:line1];
    
    
    
    UILabel *lable2 = [[UILabel alloc]initWithFrame:CGRectMake(20 * KscreenWidth, 170 * KscreenHeight + NAVIGATION_BAR_HEIGHT, 100 * KscreenWidth, 30 *KscreenHeight)];
    lable2.text = @"新的密码";
    lable2.font = [UIFont systemFontOfSize:18 * KscreenWidth];
    [self.view addSubview:lable2];
    
    _nePassWordTextField = [[UITextField alloc]initWithFrame:CGRectMake(120 * KscreenWidth, 170 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth - 140 * KscreenWidth, 30 * KscreenHeight)];
    _nePassWordTextField.secureTextEntry = YES;
    _nePassWordTextField.font = [UIFont systemFontOfSize:18 * KscreenWidth];
    _nePassWordTextField.placeholder = @"包含数字和字母，不少于6位";
    [self.view addSubview:_nePassWordTextField];
    
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(20 * KscreenWidth, 220 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth - 40 * KscreenWidth, 1 * KscreenHeight)];
    line2.backgroundColor = hui2Color;
    [self.view addSubview:line2];
    
    UILabel *lable3 = [[UILabel alloc]initWithFrame:CGRectMake(20 * KscreenWidth, 240 * KscreenHeight + NAVIGATION_BAR_HEIGHT, 100 * KscreenWidth, 30 *KscreenHeight)];
    lable3.text = @"重复密码";
    lable3.font = [UIFont systemFontOfSize:18 * KscreenWidth];
    [self.view addSubview:lable3];
    
    _ne2PassWordTextField = [[UITextField alloc]initWithFrame:CGRectMake(120 * KscreenWidth, 240 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth - 140 * KscreenWidth, 30 * KscreenHeight)];
    _ne2PassWordTextField.font = [UIFont systemFontOfSize:18 * KscreenWidth];
    _ne2PassWordTextField.placeholder = @"再次输入新密码";
    _ne2PassWordTextField.secureTextEntry = YES;
    [self.view addSubview:_ne2PassWordTextField];
    
    UIView *line3 = [[UIView alloc]initWithFrame:CGRectMake(20 * KscreenWidth, 290 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth - 40 * KscreenWidth, 1 * KscreenHeight)];
    line3.backgroundColor = hui2Color;
    [self.view addSubview:line3];
    
}

-(void)sendYanZhengMaAction{

    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{@"phone":_phoneNumber,
                       @"type":@"backPwd"
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Register&a=getSmsCode",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] intValue] == 200) {
            [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(handleMaxShowTimer:)
                                           userInfo:nil
                                            repeats:YES];
        }

        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
}

#pragma mark  - 验证码倒计时
- (void)handleMaxShowTimer:(NSTimer *)timer{
    
    _codeNum--;
    [_huoquYanZM setTitle:[NSString stringWithFormat:@"重新获取(%ld)",(long)_codeNum]   forState:UIControlStateNormal];
    _huoquYanZM.userInteractionEnabled = NO;
    if (_codeNum == 0) {
        NSLog(@"验证码超时");
        //        _codeNum = @"fsdsa";
        [_huoquYanZM setTitle:@"重新获取" forState:UIControlStateNormal];
        [timer invalidate];
        timer = nil;
        _codeNum = 60;
        _huoquYanZM.userInteractionEnabled = YES;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
